package cn.zbmao.web.util.mail;

import cn.zbmao.web.bean.User;
import cn.zbmao.web.config.TenderConfig;
import cn.zbmao.web.log.LoggerFactory;
import cn.zbmao.web.util.DegistUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import org.apache.commons.mail.EmailException;

import java.net.MalformedURLException;
import java.util.Date;

public class SignupMail {
	
	private static String HOST="http://42.51.7.86:8080";
	
	
	private static SendMail sendMail = new SendMail();
	
	static{
		String url = TenderConfig.objectUrl;
		if (url!=null) {
			HOST=url;
		}
	}

    /**
     * 注册时邮件发送
     * @param user
     */
	public static void sendSignupMail(User user){
		
		String mail = user.get("user_email");
		
		
		String md5 = DegistUtil.md5(Long.toString(new Date().getTime())).replace("+", "1").replace("/","2");

		user.set("user_signup_mail_md5", md5).update();
		
		//内容
		String html = "<html><div><h2>尊敬的注册用户：</h2>"
				+ "<p>感谢您注册招标系统，点击以下链接验证您的邮箱，只需一步即可尽享招标信息服务！</p>"
				+ "<a href=\""+HOST+"/signup/auth?keyid="+user.getInt("id")+"&key="+md5+"\">"+
				HOST+"/signup/auth?keyid="+user.getInt("id")+"&key="+md5+"</a>"
						+ "<p>请在48小时内完成验证，如果无法点击上面的链接，您可以复制该地址，并粘帖在浏览器的地址栏中访问。</p><div></html>";
		
		//邮件主题
		String subject = "招标系统注册邮件(系统自动邮件，请勿答复)";
		
		//被动加载
		if (sendMail==null) {
			sendMail=new SendMail();
		}
		try {
			sendMail.sendHtmlEmail(mail, html, subject);
		} catch (MalformedURLException e) {
			LoggerFactory.initEmailLogger().error("MalformedURLException: "+e);
		} catch (EmailException e) {
			e.printStackTrace();
			LoggerFactory.initEmailLogger().error("EmailException: "+e);
		}
	}


    public static void sendProcessSuccessMail(String email){
        String md5 = DegistUtil.md5(Long.toString(new Date().getTime())).replace("+", "1").replace("/","2");
        DaoFactory.getUserDao().updateUserForEmail(email,md5);
        //user.set("user_signup_mail_md5", md5).update();

        //内容
        String html = "<html><div><h2>尊敬的用户：</h2>"
                + "<p>您在招标猫提交了找回密码申请。</p>"
                + "<a href=\""+HOST+"/resetpwd/modify?key="+md5+"\">"+
                HOST+"/resetpwd/modify?key="+md5+"</a>"
                + "<p>请在12小时内完成验证，如果无法点击上面的链接，您可以复制该地址，并粘帖在浏览器的地址栏中访问。</p><div></html>";

        //邮件主题
        String subject = "招标系统邮件(系统自动邮件，请勿答复)-找回密码";

        //被动加载
        if (sendMail==null) {
            sendMail=new SendMail();
        }
        try {
            sendMail.sendHtmlEmail(email, html, subject);
        } catch (MalformedURLException e) {
            LoggerFactory.initEmailLogger().error("MalformedURLException: "+e);
        } catch (EmailException e) {
            e.printStackTrace();
            LoggerFactory.initEmailLogger().error("EmailException: "+e);
        }
    }

	
	public static void main(String[] args) {
		new SendMail().getSimpleEmail("尊敬的注册用户：\n 感谢您注册招标系统，点击以下链接验证您的邮箱，只需一步即可尽享招标信息服务！ http://opinions.xicp.net:10006/tendernews/oauth?userId=12&key=asdhfakjshdjfhdhfkajshdfasudhfajkdskjfha", "422271406@qq.com", "注册邮件");
	}
}
